﻿Imports System.Text

Public Class GumballMachine

    Private _count As Integer
    Public ReadOnly Property Count() As Integer
        Get
            Return _count
        End Get
    End Property

    Private _state As IState
    Public Property CurrentState() As IState
        Get
            Return _state
        End Get
        Set(ByVal value As IState)
            _state = value
        End Set
    End Property

    Private _noQuarterState As IState
    Public ReadOnly Property NoQuarterState() As IState
        Get
            Return _noQuarterState
        End Get
    End Property

    Private _soldOutState As IState
    Public ReadOnly Property SoldOutState() As IState
        Get
            Return _soldOutState
        End Get
    End Property

    Private _hasQuarterState As IState
    Public ReadOnly Property HasQuarterState() As IState
        Get
            Return _hasQuarterState
        End Get
    End Property

    Private _winnerState As IState
    Public ReadOnly Property WinnerState() As IState
        Get
            Return _winnerState
        End Get
    End Property

    Private _soldState As IState
    Public ReadOnly Property SoldState() As IState
        Get
            Return _soldState
        End Get
    End Property

    Private _location As String
    Public ReadOnly Property Location() As String
        Get
            Return _location
        End Get
    End Property

    Public Sub New(ByVal location As String, ByVal count As Integer)
        _soldOutState = New SoldOutState(Me)
        _noQuarterState = New NoQuarterState(Me)
        _hasQuarterState = New HasQuarterState(Me)
        _soldState = New SoldState(Me)
        _winnerState = New WinnerState(Me)

        _count = count
        If count > 0 Then
            _state = _hasQuarterState
        Else
            _state = _noQuarterState
        End If
        _location = location
    End Sub

    Public Sub InsertQuarter()
        _state.InsertQuarter()
    End Sub

    Public Sub EjectQuarter()
        _state.EjectQuarter()
    End Sub

    Public Sub TurnCrank()
        _state.TurnCrank()
        _state.Dispense()
    End Sub

    Public Sub ReleaseBall()
        Console.WriteLine("a ball comes rolling out the slot...")
        If _count <> 0 Then
            _count -= 1
        End If
    End Sub

    Public Sub Refill(ByVal count As Integer)
        _count += count
        _state = _noQuarterState
    End Sub

    Public Overrides Function ToString() As String
        Dim sb As New StringBuilder("")
        sb.AppendLine(Environment.NewLine + "Mighty Gumball, Inc.")
        sb.AppendLine("VB.NET-enabled Standing Gumball Model #2009")
        sb.Append("Inventory: " + Count.ToString + " gumball")
        If _count > 1 Then
            sb.AppendLine("s")
        End If
        sb.AppendLine("Machine is " + _state.ToString)
        Return sb.ToString
    End Function
End Class
